package com.example.smartcitydemo.ui.subway.subway.fragment

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.navigation.findNavController
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.databinding.FragmentSubwayBroadCastBinding
import com.example.smartcitydemo.viewModel.SubwayViewModel.SubWayViewModel

/**
 * @PACKAGE_NAME: com.example.smartcity_jscs.ui.subway.fragment
 * @NAME: Caleb.F
 * @DATE: 2021/8/23 23:42
 * @Email: 1919796283@qq.com
 */
class SubWayBroadCastFragment :
    BaseFragment<FragmentSubwayBroadCastBinding>(R.layout.fragment_subway_broad_cast) {

    private val subwayViewModel: SubWayViewModel by viewModels()


    override fun initView(
        myViewBinding: FragmentSubwayBroadCastBinding,
        savedInstanceState: Bundle?
    ) {
        myViewBinding.btnConfirm.run {
            if (isEnabled) {
                isEnabled = false
            }
            setOnClickListener {
                this.findNavController().navigateUp()
            }
        }


    }

    override fun initData() {
        subwayViewModel.getSubWayBroad()
    }


    override fun addObservers(bindView: FragmentSubwayBroadCastBinding) {
        subwayViewModel.subwayBroadData.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                bindView.apply {
                    tvSubwayBroadContent.text = it.data!!.htmlContent
                    tvSubwayBroadTitle.text = it.data!!.title
                }
            }
        }

        subwayViewModel.broadButtonLoadTime.observe(viewLifecycleOwner) {
            bindView.btnConfirm.apply {
                if (it == "0s") {
                    text = "同意"
                    isEnabled = true
                } else {
                    val buttonText = "同意(${it})"
                    text = buttonText
                }
            }

        }
    }


}